<?php
    include_once 'Classes/User.class.php';
    include_once 'DAO/UserDAO.class.php';

    // Elegxos eisodou
    $error = false;
    // Username: epitrepoume mono grammata kai arithmous
    $username = trim($_POST['username']);
    $regexp = '/^[a-z]*[a-z0-9]*$/i';
    if(!preg_match($regexp, $username)){
        $error = true;
    }
    // Password: Prepei na periexei toulaxiston 5 xarakthres
    $password = trim($_POST['password']);
    if(strlen($password) < 5){
        $error = true;
    }

    if($error){
        header('Location: index.php?error=1');
    }
    else{
        $userDao = new UserDAO();
        // An yparxei o xrhsths kanoume prowthisi sto profil tou
        if($userDao->search($username, sha1($password))){
            // Dhmiourgoume mia session kai anakateythinoume sto profil xrhsth
            if(session_start()){
                $_SESSION['userId'] = $userDao->retrieveId($username);
                $_SESSION['login'] = true;
                //header('Location: user.php#future');
                header('Location: index.php');
            }
        }
        else{
            header('Location: index.php?error=1');
        }
    }

?>
